/* 
  接口泛型的3个操作:
    1. 定义泛型类型: 定义接口的接口名的右侧
    2. 使用泛型: 接口内部
    3. 指定泛型对应的具体类型: 定义接口的实现类时, 在接口名的右侧
*/
(() => {

  interface CRUD<T> {
    data: T[]
    add (t: T): number
    getById(id: number): T
  }

  class User {
    id?: number; //id主键自增
    name: string; //姓名
    age: number; //年龄

    constructor (name, age) {
      this.name = name
      this.age = age
    }
  }

  class UserCRUD implements CRUD<User> {
    data: User[] = []


    add (user: User): number {
      user.id = Date.now()
      this.data.push(user)
      return user.id
    }

    getById(id: number): User {
      return this.data.find(user => user.id===id)
    }
  }

  const userCRUD = new UserCRUD()
  userCRUD.add(new User('A', 12))
  const id = userCRUD.add(new User('B', 13))
  console.log(userCRUD.data)

  console.log(userCRUD.getById(id))


})()